﻿using msBsc.AppLogic.DataAlc;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.wMall.Public
{
    public partial class wfTravelScenicList : msBsc.Cls.BPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControls();
            }
        }
        private void BindControls()
        {
            if (!IsPostBack)
            {
            }
        }

        private void GetArea()
        {
            using (msBsc.AppLogic.DataAlc.CBizCountryCity cbcc = new AppLogic.DataAlc.CBizCountryCity())
            {
                string cityId = this.GetParamHtValue("cityId");
                DataSet ds = cbcc.SelectCountryByParentId(cityId);
                if (ds == null || ds.Tables[0].Rows.Count < 1)
                {
                    return;
                }
                string isAll = this.GetParamHtValue("isAll");
                if (isAll.ToLower() == "true")
                {
                    DataRow dr = ds.Tables[0].NewRow();
                    dr["COUNTRY_CITY_ID"] = "";
                    dr["COUNTRY_CITY_NME"] = "全部";
                    ds.Tables[0].Rows.InsertAt(dr, 0);
                }
                string ret = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 1000, "");
                string destionId = this.GetParamHtValue("destionId");
                if (string.IsNullOrEmpty(destionId))
                {
                    destionId = ds.Tables[0].Rows[0]["COUNTRY_CITY_ID"].ToString();
                }
                this.ExecJScript(@"updateSelet(""destionId_select""," + ret + @",""" + destionId + @""")");

            }
        }

        //获取景点
        private void GetScenicList()
        {
            string cityId = this.GetParamHtValue("cityId");
            string countryId = this.GetParamHtValue("countryId");
            string destionId = this.GetParamHtValue("destionId");
            string pIndex = this.GetParamHtValue("pIndex");

            int pNum = 1;
            int.TryParse(pIndex, out pNum);
            //DJK_PUSH_SCENIC
            using (CBizPushScenic pushScenic = new CBizPushScenic())
            {
                DataSet dsRouts = pushScenic.GetScenicForPageview(20, pNum, destionId, cityId, countryId);
                if (dsRouts == null || dsRouts.Tables[0].Rows.Count < 1)
                {
                    this.ExecJScript("$('#travel_list').html('<li>没有该地区下的路线信息<li/>');");
                    return;
                }

                string strJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dsRouts.Tables[0], "", "", 1, 20, "");
                this.ExecJScript(@"laySvcList(" + strJson + "," + dsRouts.Tables[0].Rows[0]["DATACOUNT"] + ")");
            }
        }

        //加载推荐路线
        private void GetRecommendRoute()
        {

            string country = this.GetParamHtValue("countryId");

            string city = this.GetParamHtValue("cityId");

            string destion = this.GetParamHtValue("destionId");

            using (CBizDjkRouteBaseinfo routBaseInfo = new CBizDjkRouteBaseinfo())
            {

                DataSet dsRoute = null;
                for (int i = 1; i < 4; i++)
                {
                    dsRoute = routBaseInfo.GetRecommendRoute(country, city, destion, i);
                    if (dsRoute == null || dsRoute.Tables[0].Rows.Count < 1) { continue; }
                    string countryJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(dsRoute.Tables[0], "", "", 1, 2, "");
                    this.ExecJScript(@"loadRoutHtml(" + countryJson + "," + i + ")");
                }
            }

        }
        public void SaveJd()
        {
            //判断是否登录
            string userid = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(userid))
            {
                this.ExecJScript(@"layer.msg('请先登录后才能提交', { icon: 7 }, function () { location.href = '/wp_UserLogin.html';});");
                return;
            }
            

            string country = this.GetParamHtValue("countryId");
            string city = this.GetParamHtValue("cityId");

            string countryName = this.GetParamHtValue("countryName");
            string cityName = this.GetParamHtValue("cityName");

            string destion = this.GetParamHtValue("destionId");

            string destionName = this.GetParamHtValue("destionName");
            if (string.IsNullOrEmpty(destion))
            {
                destionName = "";
            }

            string days = this.GetParamHtValue("days");
            string jdlist = this.GetParamHtValue("jdlist");

            using (CBizDjkOrderRoute orderRoute = new CBizDjkOrderRoute())
            {

                string[] msg = orderRoute.AddOrderRouteInfo(days, jdlist, "", "", country, countryName, city, cityName, destion, destionName);

                if (Convert.ToBoolean(msg[0]))
                {
                    this.ExecJScript("success('"+msg[1]+"')"); return;
                }

                this.ExecJScript("layer.alert('" + msg[1] + "', { icon: 7 });");
            }
        }
        

        public override void DoAction()
        {
            if (this.strAction == "init")
            {
                GetArea();
                GetScenicList();
                GetRecommendRoute();
            }
            else if (this.strAction == "getroute")
            {
                GetScenicList();
            }
            else if (this.strAction == "saveRoute")
            {
                SaveJd();
            }
        }
    }
}